Skip to content

feat: add support for batched JSON-RPC requests#65

Merged
lpahlavi merged 26 commits intomainfrom
lpahlavi/defi-2565-batch-json-rpc
Jan 28, 2026
Merged

feat: add support for batched JSON-RPC requests#65
lpahlavi merged 26 commits intomainfrom
lpahlavi/defi-2565-batch-json-rpc

Conversation

@lpahlavi
Copy link
Contributor

@lpahlavi lpahlavi commented Dec 19, 2025

(DEFI-2565) Add support for JSON-RPC batch requests and responses. This mostly involves implementing existing JSON-RPC middlewares for JSON-RPC batches in addition to JSON-RPC single requests.

@lpahlavi lpahlavi force-pushed the lpahlavi/defi-2565-batch-json-rpc branch 4 times, most recently from 8f0a69c to 0309f4f Compare December 19, 2025 12:25
@lpahlavi lpahlavi force-pushed the lpahlavi/defi-2565-batch-json-rpc branch from 0309f4f to 0410582 Compare December 19, 2025 16:16
@lpahlavi lpahlavi force-pushed the lpahlavi/defi-2565-batch-json-rpc branch from 8b24b98 to dce5081 Compare January 5, 2026 14:01
@lpahlavi
Copy link
Contributor Author

lpahlavi commented Jan 6, 2026

@gregorydemay I did quite a bit of experimenting here with different solutions to make this as easy to use as possible despite all the generics involved with tower middlewares. For example I considered an enum wrapping single requests and batch requests, some traits for request/response pairs that are valid JSON-RPC calls (i.e. HttpJsonRpcRequest/HttpJsonRpcResponse and HttpBatchJsonRpcRequest/HttpBatchJsonRpcResponse), etc. In the end I think things as they are now are a pretty good tradeoff between being generic and usable, but let me know if you have any specific ideas/concerns and I can let you know why I didn't go in those directions.

@lpahlavi lpahlavi requested a review from gregorydemay January 6, 2026 16:03
@lpahlavi lpahlavi marked this pull request as ready for review January 6, 2026 16:03
@lpahlavi
Copy link
Contributor Author

lpahlavi commented Jan 12, 2026

@gregorydemay One thing in particular I thought we could discuss: JSON-RPC batch responses are not necessarily in the same order as the requests. At some point, we will need to match responses to requests using IDs. I actually thought we could do this in the response filter where we check for consistent IDs. We could then return a vector of Results ordered like the requests, where each element is either the response for the request in the same index, or some sort of a MissingResponseId error.

Generally, I think in the end we might have per-response errors and overall errors for the batch so the return type from the Service for batch JSON-RPC should probably be something like Result<Vec<Result<Response, ErrorType1>, ErrorType2>. We would then have to think what to do in cases like: some IDs are unexpected in the response, but some responses are indeed there (we don't want to return an error for the batch as a whole, but what do we do with the unexpected responses?). Maybe a different struct to store the response of a batch JSON-RPC request would then make more sense? WDYT?

Copy link
Contributor

@gregorydemay gregorydemay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @lpahlavi for starting this effort!

Copy link
Contributor Author

@lpahlavi lpahlavi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the review and input @gregorydemay! I've left some comments and made some improvements to the examples. Let me know what you think!

@lpahlavi lpahlavi requested a review from gregorydemay January 16, 2026 09:46
Copy link
Contributor

@gregorydemay gregorydemay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the improvements @lpahlavi ! some minor comments but otherwise starting to look good!

@lpahlavi lpahlavi requested a review from a team as a code owner January 26, 2026 16:20
Copy link
Contributor Author

@lpahlavi lpahlavi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the feedback @gregorydemay! I've incorporated all of your comments in, and this should now be ready for another round of review 🙏

@lpahlavi lpahlavi requested a review from gregorydemay January 27, 2026 10:25
Copy link
Contributor

@gregorydemay gregorydemay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @lpahlavi !

@lpahlavi lpahlavi merged commit 42924b6 into main Jan 28, 2026
10 checks passed
@lpahlavi lpahlavi deleted the lpahlavi/defi-2565-batch-json-rpc branch January 28, 2026 07:47
@github-actions github-actions bot mentioned this pull request Jan 30, 2026
lpahlavi added a commit that referenced this pull request Feb 2, 2026
## 🤖 New release

* `canhttp`: 0.4.0 -> 0.5.0 (✓ API compatible changes)
* `ic-canister-runtime`: 0.1.1 -> 0.1.2 (✓ API compatible changes)
* `ic-pocket-canister-runtime`: 0.2.0 -> 0.3.0 (⚠ API breaking changes)
* `ic-agent-canister-runtime`: 0.1.1 -> 0.1.2 (✓ API compatible changes)

<details><summary><i><b>Changelog</b></i></summary><p>

## `canhttp`

<blockquote>

## [0.5.0] - 2026-01-30

### Added

- Add support for batched JSON-RPC requests
([#65](#65))
- Add replicated request extension
([#70](#70))

### Changed

- Update dependencies
([#72](#72))

[0.5.0]:
https://github.com/dfinity/canhttp/compare/canhttp-0.4.0..canhttp-0.5.0
</blockquote>

## `ic-canister-runtime`

<blockquote>

## [0.1.2] - 2026-01-30

### Changed

- Update dependencies
([#72](#72))

[0.1.2]:
https://github.com/dfinity/canhttp/compare/ic-canister-runtime-v0.1.1..ic-canister-runtime-v0.1.2
</blockquote>

## `ic-pocket-canister-runtime`

<blockquote>

## [0.3.0] - 2026-01-30

### Changed

- **Breaking:** Bump `pocket-ic` to v12.0.0
([#72](#72))

[0.3.0]:
https://github.com/dfinity/canhttp/compare/ic-pocket-canister-runtime-v0.2.0..ic-pocket-canister-runtime-v0.3.0
</blockquote>

## `ic-agent-canister-runtime`

<blockquote>

## [0.1.2] - 2026-01-30

### Changed

- Update dependencies
([#72](#72))

[0.1.2]:
https://github.com/dfinity/canhttp/compare/ic-agent-canister-runtime-v0.1.1..ic-agent-canister-runtime-v0.1.2
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/release-plz/release-plz/).

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Louis Pahlavi <louis.pahlavi@dfinity.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants